package com.campus.counseling.controller;


import com.campus.counseling.entity.AIChatMessage;
import com.campus.counseling.entity.AIChatSession;
import com.campus.counseling.model.common.Result;
import com.campus.counseling.service.AIChatService;
import com.campus.counseling.util.SecurityUtils;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/ai-chat")
@RequiredArgsConstructor
@Tag(name = "AI聊天")
public class AIChatController {

    private final AIChatService aiChatService;

    @PostMapping("/session")
    @Operation(summary = "创建会话")
    public Result<AIChatSession> createSession(@RequestBody Map<String, Object> params) {
        Long userId = SecurityUtils.getUserId();
        Long testResultId = params.get("testResultId") != null ? 
            Long.valueOf(params.get("testResultId").toString()) : null;
        return Result.success(aiChatService.createSession(userId, testResultId));
    }

    @GetMapping("/sessions")
    @Operation(summary = "获取会话列表")
    public Result<List<AIChatSession>> getSessions() {
        Long userId = SecurityUtils.getUserId();
        return Result.success(aiChatService.getSessionList(userId));
    }

    @GetMapping("/messages/{sessionId}")
    @Operation(summary = "获取会话消息")
    public Result<List<AIChatMessage>> getMessages(@PathVariable Long sessionId) {
        return Result.success(aiChatService.getSessionMessages(sessionId));
    }

    @PostMapping("/message")
    @Operation(summary = "发送消息")
    public Result<AIChatMessage> sendMessage(@RequestBody Map<String, Object> params) {
        Long sessionId = Long.valueOf(params.get("sessionId").toString());
        String content = params.get("content").toString();
        return Result.success(aiChatService.sendMessage(sessionId, content));
    }

    @PutMapping("/session/{sessionId}/rename")
    @Operation(summary = "重命名会话")
    public Result<Void> renameSession(
            @PathVariable Long sessionId,
            @RequestBody Map<String, String> params) {
        String title = params.get("title");
        aiChatService.updateSessionTitle(sessionId, title);
        return Result.success();
    }

    @DeleteMapping("/session/{sessionId}")
    @Operation(summary = "删除会话")
    public Result<Void> deleteSession(@PathVariable Long sessionId) {
        aiChatService.deleteSession(sessionId);
        return Result.success();
    }
} 